package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.dotstone.chipism.R;
import com.dotstone.chipism.adapter.KeyPairAdapter;
import com.dotstone.chipism.adapter.KeySelectAdapter;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.bean.Key;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.bean.MySocket.DisconnectListener;
import com.dotstone.chipism.bean.MySocket.RecDataCallBackListener_Study;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.KeyManager;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DIYActivity1 extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private TextView mTitleTv;
	private Button mFinishB;
	private Button mEnsureB;
	private ImageView mTypeImg;
	private TextView mTypeTv;
	private Dialog dialog;
	private Dialog dialog1;
	private Dialog dialog2;
	private Dialog dialog3;
	private Dialog dialog4;
	private Dialog dialog5;
	private Dialog dialog6;
	private Dialog dialog7;
	private TextView mProgressTv;
	private Button mCancelB1;
	private Button mCancelB2;
	private Button mCancelB3;
	private Button mEnsureB1;
	private Button mEnsureB2;
	private Button mCancelB4;
	private Button mEnsureB4;
	private Button mCancelB5;
	private Button mEnsureB5;
	private Button mJiaB;
	private Button mJianB;
	private TextView mTempTv;
	private Button mModeB;
	private Button quxiao;
	private Button queding;
	private RadioGroup mModeGroup;
	private Button quxiao1;
	private Button queding1;
	private RadioGroup mWindGroup;
	private Button quxiao2;
	private Button queding2;
	private int tempValue = 22;
	private String temp;
	private String mode;
	private TextView mKeyCountTv;
	private LinearLayout mTestL;
	private ListView mPairedKeyL; // 已完成配置的按键列表
	private ListView mSelectKeyL; // 供用户选择所需配置的按键列表
	private List<Key> mPairedKeys = new ArrayList<Key>();
	private List<Key> mSelectKeys = new ArrayList<Key>();
	private MySocket socket;
	private boolean intercepting;
	private int num;
	private int count = 0;
	private String cmd;
	private String code;
	private String ir_data;
	private int type;
	private int time;
	private KeyPairAdapter mPairAdapter;
	private KeySelectAdapter mSelectAdapter;
	private Key mSelectedkey;
	public static DIYActivity1 diyActivity1;

	// private HashMap<Integer, String> irDataMaps = new HashMap<Integer,
	// String>();
	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.finish_btn:
			// 判断是否已经学习了按键
			// 如果没有 则提示尚未配置 无法保存
			// 如果有 则保存按键至keyManager
			// 跳转至保存界面
			if (mPairedKeys.size() == 0) {
				ToastShow.Show(getApplicationContext(), "尚未配置按键");
			} else {
				List<Key> keys = mPairAdapter.getData();
				KeyManager.getInstance().setDIYKeyList(keys);
				// KeyManager.getInstance().setDIYKeyList(mPairedKeys);
				Intent intent = new Intent(DIYActivity1.this, DIYActivity2.class);
				intent.putExtra("type", type);
				startActivity(intent);
			}
			break;
		case R.id.ensure_btn:
			// 开始配置
			// 弹出正在侦听弹出框
			// 开启线程改变句号 同时做超时判断
			startInterception();
			break;
		case R.id.cancel_btn1:
			// 取消弹出框
			// 取消监听
			dialog.dismiss();
			break;
		case R.id.cancel_btn2:
			dialog1.dismiss();
			break;
		case R.id.ensure_btn1:
			dialog1.dismiss();
			showKeyPairDialog();
			break;
		case R.id.cancel_btn3:
			dialog2.dismiss();
			break;
		case R.id.ensure_btn2:
			startInterception();
			break;
		case R.id.cancel_btn4:
			dialog3.dismiss();
			break;
		case R.id.ensure_btn4:
			boolean e = false;
			for (int i = 0; i < mPairedKeys.size(); i++) {
				if (mPairedKeys.get(i).getKeyNum() == mSelectedkey.getKeyNum()) {
					e = true;
				}
			}
			if (e) {
				ToastShow.Show(getApplicationContext(), "该按键已配置");
				return;
			} else {
				dialog3.dismiss();
				// mPairAdapter.notifyDataSetChanged();
				showPairFinish();

			}
			// irDataMaps.put(mSelectedkey.getKeyNum(), ir_data);
			break;
		case R.id.cancel_btn5:
			dialog4.dismiss();
			mSelectedkey.setCodeValue(ir_data);
			mPairedKeys.add(mSelectedkey);
			mPairAdapter.notifyDataSetChanged();
			if (mPairedKeys.size() > 0) {
				mEnsureB.setText("DIY下一键");
			} else {
				mEnsureB.setText("开始DIY");
			}
			break;
		case R.id.ensure_btn5:
			dialog4.dismiss();
			mSelectedkey.setCodeValue(ir_data);
			mPairedKeys.add(mSelectedkey);
			mPairAdapter.notifyDataSetChanged();
			if (mPairedKeys.size() > 0) {
				mEnsureB.setText("DIY下一键");
			} else {
				mEnsureB.setText("开始DIY");
			}
			break;
		case R.id.test_l:
			String cmd = ConvertUtil.getInstance().convertInfraredCmd(ir_data);
			socket.sendMqttData(cmd);
			break;
		case R.id.quxiao:
			dialog5.dismiss();
			break;
		case R.id.queding:
			temp = mTempTv.getText().toString();
			tempValue = Integer.parseInt(temp);
			mode = mModeB.getText().toString();
			Log.i("wmy", "tempValue = " + tempValue + " mode = " + mode);
			dialog5.dismiss();
			break;
		case R.id.quxiao1:
			dialog6.dismiss();
			break;
		case R.id.queding1:
			// dialog3.dismiss();
			// dialog6.dismiss();
			boolean e1 = false;
			for (int i = 0; i < mPairedKeys.size(); i++) {
				if (mPairedKeys.get(i).getKeyNum() == mSelectedkey.getKeyNum()) {
					e1 = true;
				}
			}
			if (e1) {
				ToastShow.Show(getApplicationContext(), "该按键已配置");
				return;
			} else {
				dialog3.dismiss();
				// mPairAdapter.notifyDataSetChanged();
				dialog6.dismiss();
				showPairFinish();

			}
			break;
		case R.id.quxiao2:
			dialog7.dismiss();
			break;
		case R.id.queding2:
			boolean e2 = false;
			for (int i = 0; i < mPairedKeys.size(); i++) {
				if (mPairedKeys.get(i).getKeyNum() == mSelectedkey.getKeyNum()) {
					e2 = true;
				}
			}
			if (e2) {
				ToastShow.Show(getApplicationContext(), "该按键已配置");
				return;
			} else {
				dialog3.dismiss();
				// mPairAdapter.notifyDataSetChanged();
				dialog7.dismiss();
				showPairFinish();

			}
			break;
		case R.id.jia:
			tempValue++;
			mTempTv.setText(tempValue + "");
			break;
		case R.id.jian:
			tempValue--;
			mTempTv.setText(tempValue + "");
			break;
		case R.id.mode_btn1:
			// dialog6.show();
			// modeSelectionDialog();
			if (mModeB.getText().toString().equals("制冷")) {
				mModeB.getText().toString().equals("取暖");
			} else {
				mModeB.getText().toString().equals("制冷");
			}
			break;
		default:
			break;
		}
	}

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				// 计时用
				num = msg.arg1 % 3;
				switch (num) {
				case 0:
					mProgressTv.setText(getString(R.string.interception1));
					break;
				case 1:
					mProgressTv.setText(getString(R.string.interception2));
					break;
				case 2:
					mProgressTv.setText(getString(R.string.interception3));
					break;
				default:
					break;
				}
				break;
			case 1:
				// 超时 取消侦听
				// 弹出超时dialog
				intercepting = false;
				dialog.dismiss();
				showTimeout();
				break;
			case 2:
				// 侦听成功
				// 侦听成功的dialog消失
				// 显示
				intercepting = false;
				dialog.dismiss();
				showReceived();
				break;

			default:
				break;
			}
		}
	};

	private void startInterception() {
		checkTimeOut();
		dialog.setContentView(R.layout.dialog_interception);
		Window dialogWindow = dialog.getWindow();
		mProgressTv = (TextView) dialog.findViewById(R.id.interception_tv);
		mCancelB1 = (Button) dialog.findViewById(R.id.cancel_btn1);
		mCancelB1.setOnClickListener(this);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		Log.e("wmy", "发送学习命令 ");
		cmd = ConvertUtil.getInstance().convertStudyCmd();
		if (socket.isLocalSuccess()) {
			socket.sendMqttData(cmd);
			dialog.show();
		} else {
			ToastShow.Show(getApplicationContext(), "网关连接已断开，请稍后重试");
		}
	}

	private void showTimeout() {
		dialog2.setContentView(R.layout.dialog_interception_timeout);
		Window dialogWindow = dialog2.getWindow();
		mCancelB3 = (Button) dialog2.findViewById(R.id.cancel_btn3);
		mCancelB3.setOnClickListener(this);
		mEnsureB2 = (Button) dialog2.findViewById(R.id.ensure_btn2);
		mEnsureB2.setOnClickListener(this);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog2.show();
	}

	private void showReceived() {
		dialog1.setContentView(R.layout.dialog_received);
		Window dialogWindow = dialog1.getWindow();
		mCancelB2 = (Button) dialog1.findViewById(R.id.cancel_btn2);
		mCancelB2.setOnClickListener(this);
		mEnsureB1 = (Button) dialog1.findViewById(R.id.ensure_btn1);
		mEnsureB1.setOnClickListener(this);
		mTestL = (LinearLayout) dialog1.findViewById(R.id.test_l);
		mTestL.setOnClickListener(this);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog1.show();
	}

	private void showKeyPairDialog() {
		dialog3.setContentView(R.layout.dialog_pair_key);
		Window dialogWindow = dialog3.getWindow();
		mCancelB4 = (Button) dialog3.findViewById(R.id.cancel_btn4);
		mCancelB4.setOnClickListener(this);
		mEnsureB4 = (Button) dialog3.findViewById(R.id.ensure_btn4);
		mEnsureB4.setOnClickListener(this);
		mSelectKeyL = (ListView) dialog3.findViewById(R.id.key_lv1);
		initSelectionKey();
		mSelectAdapter = new KeySelectAdapter(getApplicationContext(), mSelectKeys);
		mSelectKeyL.setAdapter(mSelectAdapter);
		mSelectKeyL.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3) {
				// TODO Auto-generated method stub
				for (int i = 0; i < mSelectKeys.size(); i++) {
					mSelectKeys.get(i).setIsSelected(false);
				}
				mSelectKeys.get(p).setIsSelected(true);
				mSelectedkey = mSelectKeys.get(p);
				mSelectAdapter.notifyDataSetChanged();
				if (mSelectKeys.get(p).getKeyNum() == 811 || mSelectKeys.get(p).getKeyNum() == 812) {
					tempSelectionDialog();
				}
				if (mSelectKeys.get(p).getKeyNum() == 832) {
					modeSelectionDialog();
				}
				if (mSelectKeys.get(p).getKeyNum() == 833) {
					windSelectionDialog();
				}
			}
		});
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog3.show();
	}

	private void showPairFinish() {
		dialog4.setContentView(R.layout.dialog_finish_paired);
		Window dialogWindow = dialog4.getWindow();
		mCancelB5 = (Button) dialog4.findViewById(R.id.cancel_btn5);
		mCancelB5.setOnClickListener(this);
		mEnsureB5 = (Button) dialog4.findViewById(R.id.ensure_btn5);
		mEnsureB5.setOnClickListener(this);
		mKeyCountTv = (TextView) dialog4.findViewById(R.id.finish_tv);
		mKeyCountTv.setText("完成" + (mPairedKeys.size() + 1) + "组按键信号的接收");
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog4.show();
	}

	private void tempSelectionDialog() {
		dialog5.setContentView(R.layout.dialog_temp_selection);
		Window dialogWindow = dialog5.getWindow();
		mJiaB = (Button) dialog5.findViewById(R.id.jia);
		mJianB = (Button) dialog5.findViewById(R.id.jian);
		mTempTv = (TextView) dialog5.findViewById(R.id.temp);
		mModeB = (Button) dialog5.findViewById(R.id.mode_btn1);
		quxiao = (Button) dialog5.findViewById(R.id.quxiao);
		queding = (Button) dialog5.findViewById(R.id.queding);
		mJiaB.setOnClickListener(this);
		mJianB.setOnClickListener(this);
		mModeB.setOnClickListener(this);
		quxiao.setOnClickListener(this);
		queding.setOnClickListener(this);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog5.show();
	}

	private void modeSelectionDialog() {
		dialog6.setContentView(R.layout.dialog_mode_selection);
		Window dialogWindow = dialog6.getWindow();
		mModeGroup = (RadioGroup) dialog6.findViewById(R.id.mode_group);
		quxiao1 = (Button) dialog6.findViewById(R.id.quxiao1);
		queding1 = (Button) dialog6.findViewById(R.id.queding1);
		quxiao1.setOnClickListener(this);
		queding1.setOnClickListener(this);
		mModeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				Log.i("wmy", "所选的Button变了" + group.getCheckedRadioButtonId() + " " + checkedId);
				String mode = ((Button) dialog6.findViewById(checkedId)).getText().toString();
				Log.i("wmy", "选择了" + mode);
			}
		});
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog6.show();
	}

	private void windSelectionDialog() {
		dialog7.setContentView(R.layout.dialog_wind_selection);
		Window dialogWindow = dialog7.getWindow();
		mWindGroup = (RadioGroup) dialog7.findViewById(R.id.wind_group);
		quxiao2 = (Button) dialog7.findViewById(R.id.quxiao2);
		queding2 = (Button) dialog7.findViewById(R.id.queding2);
		quxiao2.setOnClickListener(this);
		queding2.setOnClickListener(this);
		mWindGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				Log.i("wmy", "所选的Button变了" + group.getCheckedRadioButtonId() + " " + checkedId);
				String wind = ((Button) dialog7.findViewById(checkedId)).getText().toString();
				Log.i("wmy", "选择了" + wind);
			}
		});
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog7.show();
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_diy1;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mTitleTv.setText("DIY您的遥控器");
		mTypeImg = $(R.id.mode_icon_img);
		mTypeTv = $(R.id.mould_tv);
		mFinishB = $(R.id.finish_btn);
		mEnsureB = $(R.id.ensure_btn);
		mPairedKeyL = $(R.id.key_lv);
		diyActivity1 = this;
		type = getIntent().getIntExtra("type", 999);
		setTitle();
		dialog = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog1 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog2 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog3 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog4 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog5 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog6 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		dialog7 = new Dialog(DIYActivity1.this, R.style.MyDialogStyle);
		mPairAdapter = new KeyPairAdapter(getApplicationContext(), deletel, testl);
		mPairAdapter.setData(mPairedKeys);
		mPairedKeyL.setAdapter(mPairAdapter);
		boolVersion();
		mBackL.setOnClickListener(this);
		mFinishB.setOnClickListener(this);
		mEnsureB.setOnClickListener(this);
		mPairedKeyL.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				Log.i("wmy", "mPairedKeys.get(position).getIsSignLayoutShowm() = "
						+ mPairedKeys.get(position).getIsSignLayoutShowm());
				if(mPairedKeys.get(position).getIsSignLayoutShowm()==0){
					mPairedKeys.get(position).setIsSignLayoutShowm(1);
				}else{
					mPairedKeys.get(position).setIsSignLayoutShowm(0);
				}
				mPairAdapter.notifyDataSetChanged();
				if (mPairedKeys.size() > 0) {
					mEnsureB.setText("DIY下一键");
				} else {
					mEnsureB.setText("开始DIY");
				}
			}
		});
		dialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface arg0) {
				// TODO Auto-generated method stub
				Log.i("wmy", "取消dialog");
			}
		});
		dialog1.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface arg0) {
				// TODO Auto-generated method stub
				Log.i("wmy", "取消dialog1");
			}
		});
		dialog2.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface arg0) {
				// TODO Auto-generated method stub
				Log.i("wmy", "取消dialog2");
			}
		});
		socket = SocketManager.getInstance().sMap.get(DeviceManager.getInstance().getmWlanId());
		setRecall();
	}

	private void setTitle() {
		switch (type) {
		case DeviceType.DEVICE_TYPE_AIR_CONTROLLER:
			mTypeImg.setImageResource(R.drawable.machine_air_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_kongtiao) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_TV:
			mTypeImg.setImageResource(R.drawable.machine_tv_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_tv) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_FAN:
			mTypeImg.setImageResource(R.drawable.machine_fan_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_fenshan) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_AUDIO:
			mTypeImg.setImageResource(R.drawable.machine_amplifier_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_gongfang) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_SATELLITE:
			mTypeImg.setImageResource(R.drawable.machine_stb_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_jidinghe) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_DVD:
			mTypeImg.setImageResource(R.drawable.machine_dvd_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_dvd) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_PROJECTOR:
			mTypeImg.setImageResource(R.drawable.machine_projector_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_touyingyi) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_BOX2:
			mTypeImg.setImageResource(R.drawable.machine_wifibox);
			mTypeTv.setText(getString(R.string.yaokong_add_hezi) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_CAMERA:
			mTypeImg.setImageResource(R.drawable.machine_camera_tag);
			mTypeTv.setText(getString(R.string.yaokong_add_xaingji) + "模版");
			break;
		case DeviceType.DEVICE_TYPE_DENG:
			mTypeImg.setImageResource(R.drawable.machine_ir_switch);
			mTypeTv.setText(getString(R.string.yaokong_add_deng) + "模版");
			break;

		default:
			break;
		}
	}

	private void initSelectionKey() {
		switch (type) {
		case DeviceType.DEVICE_TYPE_AIR_CONTROLLER:
			mSelectKeys = KeyManager.getInstance().getAirCondKey();
			Log.i("wmy", DeviceType.DEVICE_TYPE_AIR_CONTROLLER + " mSelectKeys = " + mSelectKeys.size());
			break;
		case DeviceType.DEVICE_TYPE_DVD:
			mSelectKeys = KeyManager.getInstance().getDVDKey();
			Log.i("wmy", DeviceType.DEVICE_TYPE_DVD + " mSelectKeys = " + mSelectKeys.size());
			break;
		case DeviceType.DEVICE_TYPE_FAN:
			mSelectKeys = KeyManager.getInstance().getFanKey();
			Log.i("wmy", DeviceType.DEVICE_TYPE_FAN + " mSelectKeys = " + mSelectKeys.size());
			break;
		case DeviceType.DEVICE_TYPE_TV:
			mSelectKeys = KeyManager.getInstance().getTVKey();
			Log.i("wmy", DeviceType.DEVICE_TYPE_TV + " mSelectKeys = " + mSelectKeys.size());
			break;
		case DeviceType.DEVICE_TYPE_BOX2:
			mSelectKeys = KeyManager.getInstance().getBoxKey();
			Log.i("wmy", DeviceType.DEVICE_TYPE_BOX + " mSelectKeys = " + mSelectKeys.size());
			break;
		case DeviceType.DEVICE_TYPE_PROJECTOR:
			mSelectKeys = KeyManager.getInstance().getProjectorKey();
			Log.i("wmy", DeviceType.DEVICE_TYPE_PROJECTOR + " mSelectKeys = " + mSelectKeys.size());
			break;

		default:
			break;
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	private void checkTimeOut() {
		time = 0;
		intercepting = true;
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (intercepting) {
					try {
						Message msg = Message.obtain();
						if (time < 15) {
							msg.what = 0;
							msg.arg1 = time;
						} else {
							msg.what = 1;
						}
						mHandler.sendMessage(msg);
						Thread.sleep(1000);
						time++;
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		}).start();
	}

	private void setRecall() {
		socket.setRecDataCallBackListener_Study(new RecDataCallBackListener_Study() {

			@Override
			public void onReceiveData(String recData) {
				parseRecData(recData);
			}
		});
		socket.setOnDisconnectListener(new DisconnectListener() {

			@Override
			public void onDisconnect() {
				// TODO Auto-generated method stub
				Log.i("wmy", "在DIY的时候断开0.0");
				Message message = Message.obtain();
				message.what = 1;
				mHandler.sendMessage(message);
			}
		});
	}

	private void parseRecData(String recData) {
		try {
			Log.i("wmy", "解析数据 = " + recData);
			JSONObject jo = new JSONObject(recData);
			String g = jo.getString("g");
			JSONObject jo1 = new JSONObject(g);
			code = jo1.optInt("code") + "";
			// Log.i("wmy", "code = " + code);
			Message msg = Message.obtain();
			if (code.equals("0")) {
				// 继续解析
				// isReveiveFinished = true;
				// isReceiving = false;
				ir_data = jo1.getString("ir_data");
				Log.i("wmy", "解析得到 ir_data = " + ir_data);
				msg.what = 2;
			} else if (code.equals("2")) {
				msg.what = 1;
				Log.i("wmy", "操作超时");
			}
			mHandler.sendMessage(msg);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * DIYKeyAdapter2的子布局中有一个删除按键，为该按键设置点击事件
	 * 点击控件会获取按position所设的tag，因此可以取得该按键在集合中的position 通过position去删除该控件
	 */
	private OnClickListener deletel = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.delete_img:
				int position = (Integer) v.getTag();
				mPairedKeys.remove(position);
				updateDIYKeyListView();
				break;

			default:
				break;
			}
		}
	};

	private OnClickListener testl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.test_btn:
				int position = (Integer) v.getTag();
				String codeValue = mPairedKeys.get(position).getCodeValue();
				Log.i("wmy", "测试的码" + mPairedKeys.get(position).getName() + " " + mPairedKeys.size());
				String cmd = ConvertUtil.getInstance().convertInfraredCmd(codeValue);
				socket.sendMqttData(cmd);
				// 弹出对话框，询问用户是否观察到电器做出相应的响应，如有响应，则保存，如没有，则删除
				//showDeleteDialog(position);
				break;

			default:
				break;
			}
		}
	};

	private void updateDIYKeyListView() {
		// Log.i("Tag", "此时mDIYKeyList中有" + mDIYKeyList.size() + "个按键");
		// mPairAdapter.setData(mDIYKeyList);
		// keyListView.setAdapter(DIYKeyAdapter2);
		mPairAdapter.notifyDataSetChanged();
		if (mPairedKeys.size() > 0) {
			mEnsureB.setText("DIY下一键");
		} else {
			mEnsureB.setText("开始DIY");
		}
	}

}
